package com.zggk.iroad.mvp;



import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;


import com.blankj.utilcode.util.ToastUtils;
import com.zggk.iroad.R;
import com.zggk.iroad.api.MyAppliCation;


public abstract class BaseFragment extends Fragment {
    protected Toast mToast;// 消息提示Toast
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }
    /**
     * 常用的跳转方法
     */
    public void gotoActivity(Class<?> cls, boolean isFinish) {
        Intent intent = new Intent(getActivity(), cls);
        startActivity(intent);
        if (isFinish) {
            getActivity().finish();
        }
    }
    public void gotoActivity(Class<?> cls, Bundle bundle, boolean isFinish) {
        Intent intent = new Intent(getActivity(), cls);
        intent.putExtras(bundle);
        startActivity(intent);
        if (isFinish) {
            getActivity().finish();
        }
    }

    /**
     * 弹出消息提示
     *
     * @param msg      消息文本
     * @param duration 显示时长，长或短
     * @param gravity  弹出位置，上、中、下
     */
    protected void showToast(String msg, int duration, int gravity) {
        cancelToast();
        if (getActivity().isFinishing() || TextUtils.isEmpty(msg)) {
            return;
        }
        mToast = Toast.makeText(getActivity(), msg, duration);
        mToast.setGravity(gravity, 0, gravity == Gravity.BOTTOM ? 50 : 0);
        mToast.show();
    }

    /**
     * 取消提示
     */
    protected void cancelToast() {
        if (mToast != null) {
            mToast.cancel();
            mToast = null;
        }
    }

    @Override
    public void onDestroy() {
        cancelToast();
        super.onDestroy();
    }
}

